package com.hero.jobweb.service.impl;

import static com.hero.jobweb.config.MessagesManger.getSystemMessages;
import static com.hero.jobweb.config.MessagesManger.getSystemMessagesInt;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.hero.basicsystem.dao.ext.IUserDAOExt;
import com.hero.basicsystem.dao.ext.IUserRoleDAOExt;
import com.hero.basicsystem.dao.ext.IUserSystemDAOExt;
import com.hero.basicsystem.entity.User;
import com.hero.basicsystem.entity.Userrole;
import com.hero.basicsystem.entity.Usersystem;
import com.hero.jobweb.config.ExceptionKey;
import com.hero.jobweb.config.SystemKey;
import com.hero.jobweb.dao.ext.ITbUsersDAOExt;
import com.hero.jobweb.entity.TbUserAdapter;
import com.hero.jobweb.entity.TbUsers;
import com.hero.jobweb.entity.TbUsersExample;
import com.hero.jobweb.exception.BaseException;
import com.hero.jobweb.service.IPersonalService;

public class PersonalServiceImpl extends BaseService implements
		IPersonalService {

	private IUserDAOExt userDAO;
	private IUserRoleDAOExt userRoleDAO;
	private IUserSystemDAOExt userSystemDAO;
	private ITbUsersDAOExt tbUsersDAO;

	public IUserDAOExt getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(IUserDAOExt userDAO) {
		this.userDAO = userDAO;
	}

	public IUserRoleDAOExt getUserRoleDAO() {
		return userRoleDAO;
	}

	public void setUserRoleDAO(IUserRoleDAOExt userRoleDAO) {
		this.userRoleDAO = userRoleDAO;
	}

	public IUserSystemDAOExt getUserSystemDAO() {
		return userSystemDAO;
	}

	public void setUserSystemDAO(IUserSystemDAOExt userSystemDAO) {
		this.userSystemDAO = userSystemDAO;
	}

	public ITbUsersDAOExt getTbUsersDAO() {
		return tbUsersDAO;
	}

	public void setTbUsersDAO(ITbUsersDAOExt tbUsersDAO) {
		this.tbUsersDAO = tbUsersDAO;
	}

	public PersonalIndexEntity queryPersonalIndex(
			PersonalIndexEntity personalIndexEntity) {
		return new PersonalIndexEntity();
	}

	/**
	 * 注册个人用户
	 */
	public User jtaSavePersonalUser(User user) {
		if (existsUser(user) != null) {
			throw new BaseException(ExceptionKey.USER_NAME_EXISTS);
		}
		user.setEnglishName("");
		user.setDisplayName(user.getName());
		user.setBoneDate(new Date());
		user.setGuid(UUID.randomUUID().toString());
		user.setDescription("个人用户");
		user.setSex("女");//默认值
		user.setAddress("");
		user.setHomePhone("");
		user.setOfficePhone("");
		user.setMobile("");
		user.setFax("");
		user.setWeb("");
		user.setDuty("");
		user.setRemark("");
		user.setEnterpriseName("");
		user.setPastalCode("");
		user.setCountry("");
		user.setState("");
		user.setCity("");
		user.setUserTypeId(getSystemMessagesInt(SystemKey.DEFAULT_PERSONAL_USER_TYPE_ID));
		user.setUserTypeCode(getSystemMessages(SystemKey.DEFAULT_PERSONAL_USER_TYPE_CODE));
		user.setRegTime(new Date());
		user.setUpdateTime(new Date());
		user.setFlashTime(new Date());
		user.setStatus((short) getSystemMessagesInt(
				SystemKey.DEFAULT_PERSONAL_ACCOUNT_STATUS).intValue());
		user.setTraditionalName("");
		this.userDAO.jtaInsert(user);// BasicSystem用户保存
		int insertUserId = this.getBasicSystemDataBaseParamDAO().jtaSelectLastInsertId();
		user.setId(insertUserId);

		Usersystem userSystem = new Usersystem();
		userSystem
				.setSystemId(getSystemMessagesInt(SystemKey.DEFAULT_PERSONAL_SYSTEM_ID));
		userSystem.setUserId(insertUserId);
		this.userSystemDAO.jtaInsert(userSystem);// BasicSystem 用户使用的系统保存

		Userrole userRole = new Userrole();
		userRole.setUserId(insertUserId);
		userRole.setRoleId(getSystemMessagesInt(SystemKey.DEFAULT_PERSONAL_USER_ROLE_ID));
		this.userRoleDAO.jtaInsert(userRole);// BasicSystem 用户角色保存
		this.tbUsersDAO.jtaInsert(new TbUserAdapter(user));// JobWeb 用户保存
		return user;

	}

	public TbUsers existsUser(User user) {
		TbUsersExample tbUsersExample = new TbUsersExample();
		TbUsersExample.Criteria cri = tbUsersExample.createCriteria();
		cri.andNAMEEqualTo(user.getName());
		List<TbUsers> tbUserList = this.tbUsersDAO
				.selectByExample(tbUsersExample);
		if (!tbUserList.isEmpty()) {
			return tbUserList.get(0);
		}
		return null;
	}

}
